﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;
using Entity;
using Business; 

namespace WebApp
{
    public partial class ViewOrdersforAdmin : System.Web.UI.Page
    {
        string strConnect;
        SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter da;
        DataSet ds;
        protected void Page_Load(object sender, EventArgs e)
        {
            strConnect = "Data Source = (local); Database =ProjectSem3; Trusted_Connection = true";
            con = new SqlConnection(strConnect);
            ds = new DataSet();
            if (!Page.IsPostBack)
            {
                try
                {
                    GridView1.DataSource = load();
                    GridView1.DataBind();
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
        }
        private DataTable load()
        {
            Orders_Business p = new Orders_Business();
            DataTable dt = new DataTable();
            try
            {
                dt = p.Load();
            }
            catch (Exception ee)
            {
                throw new Exception(ee.Message);
            }
            finally
            {
                p = null;
            }

            return dt;
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            GridView1.DataSource = load();
            GridView1.DataBind();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            GridView1.DataSource = load();
            GridView1.DataBind();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Orders_Business orders = new Orders_Business();
            GridViewRow row = GridView1.Rows[e.RowIndex];
            int rowcount = 0;
            int ordersid = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
            int cusID = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text);
            string receivername = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
            string receiveraddress = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
            int receiverphone = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text);
            DateTime senddate = Convert.ToDateTime(((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text);
            DateTime receivedate = Convert.ToDateTime(((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text);
            int weight = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text);
            int provinceID = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text);
            int serID = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[9].Controls[0]).Text);
            int price = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).Text);
            string status = ((TextBox)GridView1.Rows[e.RowIndex].Cells[11].Controls[0]).Text;
            rowcount = orders.Update(ordersid, cusID, receivername, receiveraddress, receiverphone, senddate, receivedate, weight, provinceID, serID, price, status);
            GridView1.EditIndex = -1;
            GridView1.DataSource = load();
            GridView1.DataBind();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
              int id = int.Parse(GridView1.Rows[e.RowIndex].Cells[0].Text);


            // instantiate BAL
              Orders_Business orders = new Orders_Business();
            try
            {
                orders.Delete(id);

                
            }
            catch (Exception ee)
            {
               ee.Message.ToString();
            }
            finally
            {
                orders = null;
            }

            GridView1.EditIndex = -1;
            GridView1.DataSource = load();
            GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowState != DataControlRowState.Edit)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string id = e.Row.Cells[0].Text;
                    LinkButton lb = (LinkButton)e.Row.Cells[13].Controls[0];
                    if (lb != null)
                    {
                        lb.Attributes.Add("onclick", "return DeleteConfirmation('" + id + "');");
                    }
                }
            }
        }

        protected void txtSearch_TextChanged(object sender, EventArgs e)
        {

        }

        protected void btnsearch_Click(object sender, EventArgs e)
        {
            try
            {


                if (con.State == ConnectionState.Closed)
                    con.Open();
                string q = "select * from orders where ordersid = " + txtSearch.Text;
                cmd = new SqlCommand(q, con);
                cmd.CommandType = CommandType.Text;
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "orders");
                GridView1.DataSource = ds.Tables["orders"];
                GridView1.DataBind();


            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
    }
}